home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / mpeg / gdith.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  12KB  |  602 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include <math.h>
  22. #include "video.h"
  23. #include "proto.h"
  24. #include "dither.h"
  25.  
  26. /* Range values for lum, cr, cb. */
  27. int LUM_RANGE;
  28. int CR_RANGE;
  29. int CB_RANGE;
  30.  
  31. /* Array that remaps color numbers to actual pixel values used by X server. */
  32.  
  33. unsigned char pixel[256];
  34.  
  35. /* Arrays holding quantized value ranged for lum, cr, and cb. */
  36.  
  37. int *lum_values;
  38. int *cr_values;
  39. int *cb_values;
  40.  
  41. /* Declaration of global variable containing dither type. */
  42.  
  43. extern int ditherType;
  44.  
  45. /* Structures used by the X server. */
  46.  
  47. Display *display;
  48.  
  49. static XImage *ximage = NULL;
  50. static Colormap cmap;
  51. static Window window;
  52. static GC gc;
  53.  
  54.  
  55.  
  56. /*
  57.  *--------------------------------------------------------------
  58.  *
  59.  * InitColor --
  60.  *
  61.  *    Initialized lum, cr, and cb quantized range value arrays.
  62.  *
  63.  * Results: 
  64.  *      None.
  65.  *
  66.  * Side effects:
  67.  *      None.
  68.  *
  69.  *--------------------------------------------------------------
  70.  */
  71.  
  72. void
  73. InitColor()
  74. {
  75.   int i;
  76.  
  77.   for (i=0; i<LUM_RANGE; i++) {
  78.     lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
  79.   }
  80.  
  81.   for (i=0; i<CR_RANGE; i++) {
  82.     cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
  83.   }
  84.  
  85.   for (i=0; i<CB_RANGE; i++) {
  86.     cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
  87.   }
  88.  
  89. }
  90.  
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * ConvertColor --
  96.  *
  97.  *    Given a l, cr, cb tuple, converts it to r,g,b.
  98.  *
  99.  * Results:
  100.  *    r,g,b values returned in pointers passed as parameters.
  101.  *
  102.  * Side effects:
  103.  *      None.
  104.  *
  105.  *--------------------------------------------------------------
  106.  */
  107.  
  108. static void
  109. ConvertColor(l, cr, cb, r, g, b)
  110.      unsigned char l, cr, cb;
  111.      unsigned char *r, *g, *b;
  112. {
  113.   double fl, fcr, fcb, fr, fg, fb;
  114.  
  115.   fl = (double) l;
  116.   fcr =  ((double) cr) - 128.0;
  117.   fcb =  ((double) cb) - 128.0;
  118.  
  119.  
  120.   fr = fl + (1.40200 * fcb);
  121.   fg = fl - (0.71414 * fcb) - (0.34414 * fcr);
  122.   fb = fl + (1.77200 * fcr);
  123.  
  124.   if (fr < 0.0) fr = 0.0;
  125.   else if (fr > 255.0) fr = 255.0;
  126.  
  127.   if (fg < 0.0) fg = 0.0;
  128.   else if (fg > 255.0) fg = 255.0;
  129.  
  130.   if (fb < 0.0) fb = 0.0;
  131.   else if (fb > 255.0) fb = 255.0;
  132.  
  133.   *r = (unsigned char) fr;
  134.   *g = (unsigned char) fg;
  135.   *b = (unsigned char) fb;
  136.  
  137. }
  138.  
  139. #ifdef SH_MEM
  140.  
  141. int gXErrorFlag = 0;
  142.  
  143. int HandleXError(dpy, event)
  144.      Display *dpy;
  145.      XErrorEvent *event;
  146. {
  147.   gXErrorFlag = 1;
  148.  
  149.   return 0;
  150. }
  151.  
  152. void InstallXErrorHandler()
  153. {
  154.   int HandleXError();
  155.  
  156.   XSetErrorHandler(HandleXError);
  157.   XFlush(display);
  158. }
  159.  
  160. void DeInstallXErrorHandler()
  161. {
  162.   XSetErrorHandler(NULL);
  163.   XFlush(display);
  164. }
  165. #endif
  166.  
  167.  
  168. /*
  169.  *--------------------------------------------------------------
  170.  *
  171.  * ResizeDisplay --
  172.  *
  173.  *    Resizes display window.
  174.  *
  175.  * Results:
  176.  *    None.
  177.  *
  178.  * Side effects:
  179.  *      None.
  180.  *
  181.  *--------------------------------------------------------------
  182.  */
  183.  
  184. void ResizeDisplay(w, h)
  185.      int w, h;
  186. {
  187.  
  188.   if (ditherType == NO_DITHER) return;
  189.  
  190.   XResizeWindow(display, window, w, h);
  191.   XFlush(display);
  192. }
  193.  
  194.  
  195. /*
  196.  *--------------------------------------------------------------
  197.  *
  198.  * MakeWindow --
  199.  *
  200.  *    Create X Window
  201.  *
  202.  * Results:
  203.  *    Read the code.
  204.  *
  205.  * Side effects:
  206.  *      None.
  207.  *
  208.  *--------------------------------------------------------------
  209.  */
  210.  
  211. #ifdef SH_MEM
  212. int CompletionType = -1;
  213. #endif
  214.  
  215. static void 
  216. MakeWindow(name) 
  217. char *name;
  218. {
  219.   
  220.   XSizeHints hint;
  221.   unsigned int fg, bg;
  222.   char *hello = "MPEG Play";
  223.   int screen;
  224.   Window CreateFullColorWindow();
  225.  
  226.   if (ditherType == NO_DITHER) return;
  227.  
  228.   display = XOpenDisplay(name);
  229.   if (display == NULL) {
  230.     fprintf(stderr, "Can not open display\n");
  231.     exit(-2);
  232.   }
  233.  
  234. #ifdef SH_MEM
  235.   if(shmemFlag)
  236.     CompletionType = XShmGetEventBase(display) + ShmCompletion;
  237. #endif
  238.  
  239.   screen = DefaultScreen (display);
  240.   
  241.   /* Fill in hint structure */
  242.  
  243.   hint.x = 200;
  244.   hint.y = 300;
  245.   hint.width = 150;
  246.   hint.height = 150;
  247.   hint.flags = PPosition | PSize;
  248.   
  249.   /* Get some colors */
  250.   
  251.   bg = WhitePixel (display, screen);
  252.   fg = BlackPixel (display, screen);
  253.   
  254.   /* Make the window */
  255.   
  256.   if (ditherType == FULL_COLOR_DITHER) {
  257.     window = CreateFullColorWindow (display, hint.x, hint.y, hint.width, hint.height);
  258.     if (window == 0) {
  259.       fprintf (stderr, "-color option only valid on full color display\n");
  260.       exit (-1);
  261.     }
  262.   } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  263.     window = XCreateSimpleWindow (display,
  264.                   DefaultRootWindow (display),
  265.                   hint.x, hint.y,
  266.                   hint.width, hint.height,
  267.                   4, fg, bg);
  268.   } else {
  269.     XVisualInfo vinfo;
  270.     
  271.     if (!XMatchVisualInfo (display, screen, 8, PseudoColor, 
  272.                &vinfo)) {
  273.  
  274.       if (!XMatchVisualInfo(display, screen, 8, GrayScale, 
  275.                 &vinfo)) {
  276.  
  277.     fprintf(stderr, "-requires 8 bit display\n");
  278.     exit(-1);
  279.       }
  280.     }
  281.  
  282.     window = XCreateSimpleWindow (display,
  283.                  DefaultRootWindow (display),
  284.                  hint.x, hint.y,
  285.                  hint.width, hint.height,
  286.                  4, fg, bg);
  287.   }
  288.   
  289.   XSelectInput(display, window, StructureNotifyMask);
  290.  
  291.   /* Tell other applications about this window */
  292.   
  293.   XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint);
  294.   
  295.   /* Map window. */
  296.  
  297.   XMapWindow(display, window);
  298.  
  299.   /* Wait for map. */
  300.   while(1) {
  301.     XEvent    xev;
  302.  
  303.     XNextEvent(display, &xev);
  304.     if(xev.type == MapNotify && xev.xmap.event == window)
  305.       break;
  306.   }
  307.   XSelectInput(display, window, NoEventMask);
  308. }
  309.   
  310.  
  311. /*
  312.  *--------------------------------------------------------------
  313.  *
  314.  * InitDisplay --
  315.  *
  316.  *    Initialized display, sets up colormap, etc.
  317.  *
  318.  * Results:
  319.  *      None.
  320.  *
  321.  * Side effects:
  322.  *      None.
  323.  *
  324.  *--------------------------------------------------------------
  325.  */
  326.  
  327. void InitDisplay(name)
  328. char *name;
  329. {
  330.  
  331.   int ncolors = LUM_RANGE*CB_RANGE*CR_RANGE;
  332.   XColor xcolor;
  333.   int i, lum_num, cr_num, cb_num;
  334.   unsigned char r, g, b;
  335.   Colormap dcmap;
  336.  
  337.   if (ditherType == NO_DITHER) return;
  338.  
  339.   MakeWindow(name);
  340.  
  341.   gc = XCreateGC(display, window, 0, 0);
  342.  
  343.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  344.  
  345.   xcolor.flags = DoRed | DoGreen | DoBlue;
  346.  
  347.   retry_alloc_colors:
  348.   for (i=0; i<ncolors; i++) {
  349.  
  350.     lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
  351.     cr_num = (i / CB_RANGE)%CR_RANGE;
  352.     cb_num = i % CB_RANGE;
  353.  
  354.     ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num], &r, &g, &b);
  355.  
  356.     xcolor.red = r * 256;
  357.     xcolor.green = g * 256;
  358.     xcolor.blue = b * 256;
  359.  
  360.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  361.       int j;
  362.       long tmp_pixel;
  363.       XWindowAttributes xwa;
  364.  
  365.       if (!quietFlag) {
  366.     fprintf(stderr, "Using private colormap.\n");
  367.       }
  368.  
  369.       /* Free colors. */
  370.       for(j = 0; j < i; j ++) {
  371.     tmp_pixel = pixel[j];
  372.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  373.       }
  374.  
  375.       XGetWindowAttributes(display, window, &xwa);
  376.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  377.       XSetWindowColormap(display, window, cmap);
  378.  
  379.       goto retry_alloc_colors;
  380.     }
  381.     pixel[i] = xcolor.pixel;
  382.   }
  383.  
  384.   ximage = NULL;
  385. }
  386.  
  387.  
  388. /*
  389.  *--------------------------------------------------------------
  390.  *
  391.  * InitGrayDisplay --
  392.  *
  393.  *    Initialized display for gray scale dither.
  394.  *
  395.  * Results:
  396.  *      None.
  397.  *
  398.  * Side effects:
  399.  *      None.
  400.  *
  401.  *--------------------------------------------------------------
  402.  */
  403.  
  404. #define NUM_COLORS 128
  405.  
  406. void InitGrayDisplay(name)
  407. char *name;
  408. {
  409.   int ncolors = NUM_COLORS;
  410.   XColor xcolor;
  411.   int i;
  412.   Colormap dcmap;
  413.  
  414.   MakeWindow(name);
  415.  
  416.   gc = XCreateGC(display, window, 0, 0);
  417.  
  418.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  419.  
  420.   xcolor.flags = DoRed | DoGreen | DoBlue;
  421.  
  422.   retry_alloc_grays:
  423.   for (i=0; i<ncolors; i++) {
  424.  
  425.     xcolor.red = (i*2) * 256;
  426.     xcolor.green = (i*2) * 256;
  427.     xcolor.blue = (i*2) * 256;
  428.  
  429.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  430.       int j;
  431.       long tmp_pixel;
  432.       XWindowAttributes xwa;
  433.  
  434.       if (!quietFlag) {
  435.     fprintf(stderr, "Using private colormap.\n");
  436.       }
  437.  
  438.       /* Free colors. */
  439.       for(j = 0; j < i; j ++) {
  440.     tmp_pixel = pixel[j*2];
  441.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  442.       }
  443.  
  444.       XGetWindowAttributes(display, window, &xwa);
  445.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  446.       XSetWindowColormap(display, window, cmap);
  447.  
  448.       goto retry_alloc_grays;
  449.     }
  450.     pixel[(i*2)] = xcolor.pixel;
  451.     pixel[(i*2)+1] = xcolor.pixel;
  452.   }
  453.  
  454.   ximage = NULL;
  455. }
  456.  
  457.  
  458. /*
  459.  *--------------------------------------------------------------
  460.  *
  461.  * InitMonoDisplay --
  462.  *
  463.  *    Initialized display for monochrome dither.
  464.  *
  465.  * Results:
  466.  *      None.
  467.  *
  468.  * Side effects:
  469.  *      None.
  470.  *
  471.  *--------------------------------------------------------------
  472.  */
  473.  
  474. void InitMonoDisplay(name)
  475. char *name;
  476. {
  477.   XGCValues xgcv;
  478.  
  479.   MakeWindow(name);
  480.  
  481.   xgcv.background = BlackPixel(display, DefaultScreen(display));
  482.   xgcv.foreground = WhitePixel(display, DefaultScreen(display));
  483.  
  484.   gc = XCreateGC(display, window, GCForeground | GCBackground, &xgcv);
  485.  
  486.   ximage = NULL;
  487. }
  488.  
  489.  
  490. /*
  491.  *--------------------------------------------------------------
  492.  *
  493.  * InitColorDisplay --
  494.  *
  495.  *    Initialized display for full color output.
  496.  *
  497.  * Results:
  498.  *      None.
  499.  *
  500.  * Side effects:
  501.  *      None.
  502.  *
  503.  *--------------------------------------------------------------
  504.  */
  505.  
  506. void InitColorDisplay(name)
  507. char *name;
  508. {
  509.  
  510.   MakeWindow(name);
  511.  
  512.   gc = XCreateGC(display, window, 0, 0);
  513.   ximage = NULL;
  514. }
  515.  
  516.  
  517. /*
  518.  *--------------------------------------------------------------
  519.  *
  520.  * ExecuteDisplay --
  521.  *
  522.  *    Actually displays display plane in previously created window.
  523.  *
  524.  * Results:
  525.  *    None.
  526.  *
  527.  * Side effects:
  528.  *    None.
  529.  *
  530.  *--------------------------------------------------------------
  531.  */
  532.  
  533. void
  534. ExecuteDisplay(vid_stream)
  535.      VidStream *vid_stream;
  536. {
  537.   char dummy;
  538.   Visual *FindFullColorVisual();
  539.   Visual *fc_visual;
  540.   int depth;
  541.  
  542.   totNumFrames++;
  543.   if (!quietFlag) {
  544.     fprintf (stderr, "%d\r", totNumFrames);
  545.   }
  546.  
  547.   if (ditherType == NO_DITHER) return;
  548.  
  549.   if (ximage == NULL) {
  550.  
  551.     if (ditherType == Twox2_DITHER) {
  552.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  553.                 vid_stream->mb_width * 32,
  554.                 vid_stream->mb_height * 32, 8, 0);
  555.     } else if (ditherType == FULL_COLOR_DITHER) {
  556.       fc_visual = FindFullColorVisual(display, &depth);
  557.       ximage = XCreateImage (display, fc_visual, depth, ZPixmap,
  558.                  0, &dummy, vid_stream->mb_width * 16,
  559.                  vid_stream->mb_height * 16, 32, 0);
  560.     } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  561.       ximage = XCreateImage (display, None, 1, XYBitmap, 0, &dummy,
  562.                  vid_stream->mb_width * 16,
  563.                  vid_stream->mb_height * 16, 8, 0);
  564.       ximage->byte_order = MSBFirst;
  565.       ximage->bitmap_bit_order = MSBFirst;
  566.     } else {
  567.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  568.                 vid_stream->mb_width * 16,
  569.                 vid_stream->mb_height * 16, 8, 0);
  570.     }
  571.   }
  572.  
  573.   if (!noDisplayFlag) {
  574. #ifdef SH_MEM
  575.     if (shmemFlag) {
  576.       XShmPutImage(display, window, gc, vid_stream->current->ximage, 
  577.            0, 0, 0, 0,
  578.            vid_stream->current->ximage->width, 
  579.            vid_stream->current->ximage->height, True);
  580.       XFlush(display);
  581.       
  582.       while(1) {
  583.     XEvent xev;
  584.     
  585.     XNextEvent(display, &xev);
  586.     if(xev.type == CompletionType)
  587.       break;
  588.       }
  589.     }
  590.     else 
  591. #endif
  592.       
  593.       {
  594.     ximage->data = (char *) vid_stream->current->display; 
  595.     
  596.     XPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
  597.       }
  598.   }
  599. }
  600.  
  601.  
  602.